package Main;

import util.FileUtil;

import javax.swing.event.TreeExpansionListener;
import java.io.*;
import java.util.*;

/**
 * Created by zxp on 2016/11/17.
 */
public class FinalGenerate {


    static Map<String, Integer> termMap = new HashMap<>();

    static Map<String, Integer> conferenceMap = new HashMap<>();

    static Map<String, Integer> timeMap = new HashMap<>();

    static Map<String, Integer> docMap = new HashMap<>();


    /**
     * 生成编号表示的tensor文件（即：每个词都用它在词典的索引表示，会议和时间也类似）
     */
    public static void finalGenerateTensor() {

        BufferedReader br = null;

        try {
            int count = 1;
            br = new BufferedReader(new InputStreamReader(new FileInputStream(Constraint.dicFile), "UTF-8"));
            String line;
            while ((line = br.readLine()) != null) {
                termMap.put(line, count);
                count++;
            }
            br.close();
            System.out.println("term数量：" + (count - 1));

            count = 1;
            br = new BufferedReader(new InputStreamReader(new FileInputStream(Constraint.timeDicFile), "UTF-8"));
            while ((line = br.readLine()) != null) {
                timeMap.put(line, count);
                count++;
            }
            br.close();
            System.out.println("time数量：" + (count - 1));

            count = 1;
            br = new BufferedReader(new InputStreamReader(new FileInputStream(Constraint.conferenceDicFile), "UTF-8"));
            while ((line = br.readLine()) != null) {
                conferenceMap.put(line, count);
                count++;
            }
            br.close();
            System.out.println("conference数量：" + (count - 1));

            count = 1;
            br = new BufferedReader(new InputStreamReader(new FileInputStream(Constraint.docDicFile), "UTF-8"));
            while ((line = br.readLine()) != null) {
                docMap.put(line, count);
                count++;
            }
            br.close();
            System.out.println("doc数量：" + (count - 1));


            BufferedWriter bw = FileUtil.initBufferedWriter(Constraint.figureTensorFile);
            br = new BufferedReader(new InputStreamReader(new FileInputStream(Constraint.tensorFile), "UTF-8"));
            while ((line = br.readLine()) != null) {
                String[] arr = line.split(" ");
                String time = arr[0];
                String conference = arr[1];
                String term = arr[2];
                int value = Integer.valueOf(arr[3]);

                bw.write(termMap.get(term) + " " + conferenceMap.get(conference) + " " + timeMap.get(time) + " " + value);
                bw.newLine();
            }
            br.close();
            bw.flush();
            bw.close();


            bw = FileUtil.initBufferedWriter(Constraint.figureDFile);
            br = new BufferedReader(new InputStreamReader(new FileInputStream(Constraint.DFile), "UTF-8"));
            while ((line = br.readLine()) != null) {
                String[] arr = line.split(" ");
                String doc = arr[0];
                String term = arr[1];
                int value = Integer.valueOf(arr[2]);

                bw.write(termMap.get(term) + " " + docMap.get(doc) + " " + value);
                bw.newLine();
            }
            br.close();
            bw.flush();
            bw.close();


        } catch (IOException e) {
            e.printStackTrace();
        }

    }





    public static void main(String[] args) {
        finalGenerateTensor();

    }


}
